package com.gxa.p2p.service.impl;

import com.gxa.p2p.mapper.PlatFormMapper;
import com.gxa.p2p.pojo.*;
import com.gxa.p2p.service.PlatFormService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author xhy
 * @Date 2021/10/26 14:45
 */
@Transactional(rollbackFor = {Throwable.class})
@Service("platFormService")
public class PlatFormServiceImpl implements PlatFormService {

    @Autowired
    private PlatFormMapper platformMapper = null;


    @Override
    public List<Platform> findAllPlatForm(Long id, String type, String outOrIn, Integer page, Integer size) {
        return platformMapper.findAllPlatForm(id, type, outOrIn, (page-1)*size, size);
    }

    @Override
    public List<Platform> findAllPlatForm(Long id, String type, String outOrIn) {
        return platformMapper.findAllPlatForm(id, type, outOrIn,null,null);
    }

    @Override
    public Double findPlatformBalance() {
        return platformMapper.findPlatformBalance();
    }

    @Override
    public void insertPlatformRecharge(Platform platform) {
        platformMapper.insertPlatformRecharge(platform);
    }

    @Override
    public List<String> findALlMoneyType() {
        List<String> list = platformMapper.selectInvestrecordType();
        List<String> list1 = platformMapper.selectOutRecordrecordType();
        List<String> list2 = platformMapper.selectRechargeRecordrecordType();
        List<String> list3 = platformMapper.selectRepayrecordType();
        for (int i=0;i<list1.size();i++){
            list.add(list1.get(i));
        }
        for (int i=0;i<list2.size();i++){
            list.add(list2.get(i));
        }
        for (int i=0;i<list3.size();i++){
            list.add(list3.get(i));
        }
        return list;
    }

    @Override
    public List<UserRecordVo> findPlatfromlog(String name, String phone, String type, Integer page, Integer size) {
        List<UserRecordVo> borrowOutMoneyLog = platformMapper.findBorrowOutMoneyLog(name, phone, type, null,null);
        List<UserRecordVo> investMoneyLog = platformMapper.findInvestMoneyLog(name, phone, type, null,null);
        List<UserRecordVo> investOutMoneyLog = platformMapper.findInvestOutMoneyLog(name, phone, type, null,null);
        List<UserRecordVo> rechargeLog = platformMapper.findRechargeLog(name, phone, type, null,null);
        List<UserRecordVo> reparLog = platformMapper.findReparLog(name, phone, type, null,null);
        for (int i=0;i<investMoneyLog.size();i++){
            borrowOutMoneyLog.add(investMoneyLog.get(i));
        }
        for (int i=0;i<investOutMoneyLog.size();i++){
            borrowOutMoneyLog.add(investOutMoneyLog.get(i));
        }
        for (int i=0;i<rechargeLog.size();i++){
            borrowOutMoneyLog.add(rechargeLog.get(i));
        }
        for (int i=0;i<reparLog.size();i++){
            borrowOutMoneyLog.add(reparLog.get(i));
        }
        List<UserRecordVo> list = new ArrayList<>();
        for(int i=(page-1)*size;i<(page-1)*size+size;i++){
            if (i<=borrowOutMoneyLog.size()-1){
                list.add(borrowOutMoneyLog.get(i));
                System.out.println("----------");
            }
            if (i>=borrowOutMoneyLog.size()-1){
                return list;
            }
        }
        return list;
    }

    @Override
    public List<UserRecordVo> findPlatfromlog(String name, String phone, String type) {
        List<UserRecordVo> borrowOutMoneyLog = platformMapper.findBorrowOutMoneyLog(name, phone, type, null, null);

        List<UserRecordVo> investMoneyLog = platformMapper.findInvestMoneyLog(name, phone, type,null, null);

        List<UserRecordVo> investOutMoneyLog = platformMapper.findInvestOutMoneyLog(name, phone, type, null, null);

        List<UserRecordVo> rechargeLog = platformMapper.findRechargeLog(name, phone, type, null, null);

        List<UserRecordVo> reparLog = platformMapper.findReparLog(name, phone, type, null, null);

        for (int i=0;i<investMoneyLog.size();i++){
            borrowOutMoneyLog.add(investMoneyLog.get(i));
        }
        for (int i=0;i<investOutMoneyLog.size();i++){
            borrowOutMoneyLog.add(investOutMoneyLog.get(i));
        }
        for (int i=0;i<rechargeLog.size();i++){
            borrowOutMoneyLog.add(rechargeLog.get(i));
        }
        for (int i=0;i<reparLog.size();i++){
            borrowOutMoneyLog.add(reparLog.get(i));
        }
        return borrowOutMoneyLog;
    }

    @Override
    public List<UserMoneyVo> findInvestAndBorrowUser(String name, String phone, String type, Integer page, Integer size) {
        List<UserMoneyVo> investAndBorrowUser = platformMapper.findInvestAndBorrowUser(name, phone, type, (page-1)*size, size);
        return investAndBorrowUser;
    }

    @Override
    public List<UserMoneyVo> findInvestAndBorrowUser(String name, String phone, String type) {
        return platformMapper.findInvestAndBorrowUser(name, phone, type,null,null);
    }

    @Override
    public void platWithdrawal(Outrecord outrecord) {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmm");
        String str=sdf.format(new Date());
        outrecord.setBankSerial(str);
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        timestamp.setHours(timestamp.getHours()+3);
        outrecord.setCheckTime(timestamp);
        outrecord.setGetTime(timestamp);
        platformMapper.platWithdrawal(outrecord);
    }

    @Override
    public void platRecharge(Rechargerecord rechargerecord) {
        // (new Timestamp(System.currentTimeMillis()).setHours((new Timestamp(System.currentTimeMillis()).getHours()+1)))
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        timestamp.setHours(timestamp.getHours()+3);
        rechargerecord.setRecievedTime(timestamp);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmm");
        String str=sdf.format(new Date());
        rechargerecord.setSerialNumber(str);
        platformMapper.platRecharge(rechargerecord);
    }

    @Override
    public void rechargeInvestUserMoney(String phone, Double money) {
        platformMapper.rechargeInvestUserMoney(phone, money);
    }

    @Override
    public void outInvestUserMoney(String phone, Double money) {
        platformMapper.outInvestUserMoney(phone, money);
    }

    @Override
    public List<Outrecord> exportWithdrawlogByPage(Long id, String userPhone, String userName, String bankId, String bankName, String from, String to) {
        return platformMapper.findWithdrawlog(id, userPhone, userName, bankId, bankName, from, to, null,null);
    }

    @Override
    public void updateAuditState(Long id, String state,String remarks) {
        platformMapper.updateAuditState(id, state,remarks);
    }


    @Override
    public List<Outrecord> findWithdrawlog(Long id, String userPhone, String userName, String bankId, String bankName, String from, String to, Integer page,Integer size) {
        List<Outrecord> outrecords = platformMapper.findWithdrawlog(id, userPhone, userName, bankId, bankName, from, to, size*(page-1),size);
        return outrecords;
    }

    @Override
    public Map selectOutrecordUser(Long id) {
        Map map = platformMapper.selectOutrecordUser(id);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String str=sdf.format(map.get("submitTime"));
        map.put("submitTime",str);
        return map;
    }

    @Override
    public List<Outrecord> selectRecentOutrecord(Long id) {
        return platformMapper.selectRecentOutrecord(id);
    }
}
